extends Node

func _ready() -> void:
	DisplayServer.window_set_size(Global.W_resolution)
	
	$Achievement.init()
	 
	menu()

func _process(_delta) -> void:
	if !(Global.is_game_running or Global.is_game_over):
		if Input.is_action_just_pressed("Comfirm"):
			start()
		return
		
	if Input.is_action_just_pressed("Pause") and !Global.is_game_paused:
		pause()

func menu() -> void:
	reset()
	$UILayer.hide_all_menus()
	$UILayer/StartMenu.show()
	

func reset() -> void:
	# Reset the whole game

	get_tree().paused = false

	Global.is_game_paused = false
	Global.is_game_running = false
	Global.is_game_over = false

	# Reset game variables	
	Global.G_trail_color = Global._trail_color.duplicate(true)
	if get_node("%Achievement").is_achieved("Ending1"):
		Global.G_scrollSpeed = Global.G_baseScrollSpeed * 1.3
		$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1.2
		$"/root/Game/Mechanics/DayNightSwitchTimer".wait_time = 25
	else:
		Global.G_scrollSpeed = Global.G_baseScrollSpeed
		$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1.5
	Global.G_natural_score_increase = 10
	Global.G_score = 0
	Global.G_phase = Global.Phase.Day
	Global.G_entered_night = false
	
	
	$Bird.reset()
	$Bird.show()
	$Bird/BeamCooldown.stop()
	$Bird/BeamCooldown.wait_time = $Bird/BeamCooldown.CD
	$Bird/CollisionDisabledTimer.stop()
	$Bird/CollisionShape2D.disabled = false

	get_tree().call_group("obstacle", "queue_free")
	get_tree().call_group("particles", "queue_free")

	$Mechanics.stop_all_timers()

	# Reset UI
	$UILayer/GamingUI/Score.text = "Score 0"
	$UILayer/GamingUI/Beam.texture = Preload.LightBeamSkill
	$UILayer/GamingUI/Beam/BeamCooldown.text = "0"
	$BackgroundLayer/BackgroundImage.texture = Preload.Background_Day[0]

	$GlobalLightLevel.set_color(Color(0, 0, 0, 0))
	
	# Music
	$Music.stop_all_music()
	$Music/DayMusic.play()

	# Misc
	$ObstacleFactory.next_spawn_interfered = false
	$ParticleFactory.CurrentBeamType = $ParticleFactory.BeamType.Light
	$Achievement/Rainbow/Cat.cat_hide()
	$Achievement/enter_night_once.is_achieved = false 
	
func start() -> void: 	 	
	# Called after reset()

	Global.is_game_running = true

	$Bird.flying = true
	$Bird.flap()
	
	$UILayer.hide_all_menus()
	$UILayer/GamingUI.show()
	$UILayer/GamingUI/MouseHint.show()

	$Mechanics.start_all_timers()

	if get_node("%Achievement").is_achieved("Ending1"):
		$Mechanics/DayNightSwitchTimer.start()
	
func pause() -> void:
	Global.is_game_paused = true

	$UILayer/PauseMenu.show()
	$Music.pause_current_music()
	get_tree().paused = true

func unpause() -> void:
	Global.is_game_paused = false

	$UILayer/PauseMenu.hide()
	$Music.unpause_current_music()
	get_tree().paused = false

func end() -> void:
	Global.is_game_running = false
	Global.is_game_over = true

	Global.G_best_score = max(Global.G_score, Global.G_best_score)
	$UILayer/GameOverMenu.show()
	$UILayer/GameOverMenu.random_tips_pick() 
	$UILayer/AchievementsMenu/BestScore.text = "Best Score: " + str(Global.G_best_score)
	$Music.stop_all_music()

	get_tree().paused = true # Pause the game
